<template>
<h1>发布微博页面</h1>
  <input type="text" placeholder="说点什么..." v-model="weibo.content">
  <input type="button" value="发微博" @click="postBlog()">
</template>

<script setup>
import {ref} from "vue";
import router from "@/router";
import axios from "axios";

const weibo=ref({content:""});
const postBlog=()=>{
  //得到当前登录的用户
  let user=localStorage.user?JSON.parse(localStorage.user):null;
  if (user==null){
    alert("请先登录")
    router.push('/login')
    return;
  }
  //把用户登录的id保存到weibo对象中
  weibo.value.userId=user.id;
  //发请求
  axios.post('http://localhost:8080/v1/weibos/add-new',weibo.value)
      .then((response)=>{
        if (response.data.code==1){
          alert("发布完成!");
          router.push('/');//返回首页
        }
      })
}
</script>

<style scoped>

</style>